import java.util.Random;
import java.util.HashSet;
import java.util.Set;

/**
 * 加减法口算练习系统 v0.3
 * 避免重复算式，控制加减法比例
 */
public class CalculationSystemV03 {

    private static Random random = new Random();
    private static Set<String> equationSet = new HashSet<>();

    /**
     * 生成指定范围内的随机数
     */
    public static int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成有效的加法算式（和不超过100）
     */
    public static String generateAdditionEquation() {
        int num1, num2, sum;

        do {
            num1 = generateRandomNumber(0, 100);
            num2 = generateRandomNumber(0, 100);
            sum = num1 + num2;
        } while (sum > 100);

        return String.format("%2d + %2d = %3d", num1, num2, sum);
    }

    /**
     * 生成有效的减法算式（差不小于0）
     */
    public static String generateSubtractionEquation() {
        int num1, num2, difference;

        do {
            num1 = generateRandomNumber(0, 100);
            num2 = generateRandomNumber(0, 100);
            difference = num1 - num2;
        } while (difference < 0);

        return String.format("%2d - %2d = %3d", num1, num2, difference);
    }

    /**
     * 检查算式是否重复
     */
    public static boolean isEquationDuplicate(String equation) {
        return equationSet.contains(equation);
    }

    /**
     * 生成不重复的算式，控制加减法比例
     */
    public static String generateUniqueEquation(double additionRatio) {
        String equation;
        int maxAttempts = 100; // 防止无限循环

        for (int attempt = 0; attempt < maxAttempts; attempt++) {
            // 根据比例决定生成加法还是减法
            boolean isAddition = random.nextDouble() < additionRatio;

            if (isAddition) {
                equation = generateAdditionEquation();
            } else {
                equation = generateSubtractionEquation();
            }

            // 检查是否重复
            if (!isEquationDuplicate(equation)) {
                equationSet.add(equation);
                return equation;
            }
        }

        // 如果尝试多次仍然重复，返回任意一个
        boolean isAddition = random.nextDouble() < additionRatio;
        return isAddition ? generateAdditionEquation() : generateSubtractionEquation();
    }

    /**
     * 生成指定数量的不重复算式
     */
    public static String[] generateUniqueEquations(int count, double additionRatio) {
        String[] equations = new String[count];
        equationSet.clear(); // 清空之前的记录

        for (int i = 0; i < count; i++) {
            equations[i] = generateUniqueEquation(additionRatio);
        }
        return equations;
    }

    /**
     * 格式化输出算式
     */
    public static void printEquations(String[] equations, int perLine) {
        System.out.println("序号\t算式");
        System.out.println("----\t----------------");

        for (int i = 0; i < equations.length; i++) {
            System.out.printf("%2d.\t%s\t", i + 1, equations[i]);
            if ((i + 1) % perLine == 0) {
                System.out.println();
            }
        }
        if (equations.length % perLine != 0) {
            System.out.println();
        }
    }

    /**
     * 统计算式信息
     */
    public static void printStatistics(String[] equations) {
        int additionCount = 0;
        int subtractionCount = 0;

        for (String eq : equations) {
            if (eq.contains("+")) {
                additionCount++;
            } else {
                subtractionCount++;
            }
        }

        System.out.println("统计信息:");
        System.out.println("- 加法算式: " + additionCount + " 道");
        System.out.println("- 减法算式: " + subtractionCount + " 道");
        System.out.println("- 加法比例: " + String.format("%.1f%%", additionCount * 100.0 / equations.length));
        System.out.println("- 重复检查: " + (equationSet.size() == equations.length ? "无重复" : "可能有重复"));
    }

    public static void main(String[] args) {
        System.out.println("加减法口算练习系统 v0.3");
        System.out.println("======================");

        // 生成50道不重复算式，加法比例60%
        String[] equations = generateUniqueEquations(50, 0.6);

        // 每行显示3个算式
        printEquations(equations, 3);

        System.out.println("======================");
        System.out.println("共生成 " + equations.length + " 道不重复算式");
        printStatistics(equations);
    }
}